package com.fjwt.gz.mch.mq;

import com.fjwt.gz.components.mq.model.Acte017SendCouponMQ;
import com.fjwt.gz.core.cache.RedisUtil;
import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.db.bo.ActivityUserTimesBO;
import com.fjwt.gz.db.dto.FansActivityDTO;
import com.fjwt.gz.db.entity.FansAuthEntity;
import com.fjwt.gz.service.activity.platform.acte017.ACTE017Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 接收MQ消息
 * 业务： 越减越便宜消费[ 参与活动后发放卡券 ]
 *
 */
@Slf4j
@Component
public class Acte017SendCouponMQReceiver implements Acte017SendCouponMQ.IMQReceiver {

    @Autowired
    private ACTE017Service acte017Service;

    private final long redisOutTime = 5 * 60 * 1000;

    @Override
    public void receive(Acte017SendCouponMQ.MsgPayload payload) {
        try {
            FansActivityDTO fansActivityDTO = new FansActivityDTO();
            BeanUtils.copyProperties(payload, fansActivityDTO);
            ActivityUserTimesBO activityUserTimesBO = new ActivityUserTimesBO();
            if (ObjectUtils.isNotEmpty(payload.getActivityUserTimesBO())) {
                BeanUtils.copyProperties(payload.getActivityUserTimesBO(), activityUserTimesBO);
                fansActivityDTO.setActivityUserTimesBO(activityUserTimesBO);
            }
            if (ObjectUtils.isNotEmpty(payload.getFansAuthEntity())) {
                FansAuthEntity fansAuthEntity = new FansAuthEntity();
                BeanUtils.copyProperties(payload.getFansAuthEntity(), fansAuthEntity);
                fansActivityDTO.setFansAuthEntity(fansAuthEntity);
            }
            acte017Service.handleActivityAwardBO(fansActivityDTO);
        }catch (Exception e) {
            log.error("消费异常：{}", e.getMessage());
            RedisUtil.setString(Constants.REDIS_ACTIVITY.FILENAME + payload.getActivityId() + ":consumeError:" + payload.getFansId(), e.getMessage(), redisOutTime, TimeUnit.MILLISECONDS);
            e.printStackTrace();
        }
    }

}
